// 图表管理类 - 简约设计
class ChartManager {
    constructor() {
        this.charts = {};
        this.init();
    }

    // 初始化所有图表
    init() {
        this.initCreditDistribution();
        this.initDepartmentCompletion();
        this.bindResizeEvents();
    }

    // 初始化学分分布统计图表 - 简约设计
    initCreditDistribution() {
        const chartDom = document.getElementById('creditDistribution');
        if (!chartDom) return;

        this.charts.creditDistribution = echarts.init(chartDom);
        
        const option = {
            animation: true,
            animationDuration: 1000,
            animationEasing: 'ease',
            title: {
                text: '学分分布统计',
                left: 'center',
                textStyle: {
                    fontSize: 16,
                    fontWeight: 'normal',
                    color: '#374151'
                }
            },
            tooltip: {
                trigger: 'axis',
                backgroundColor: '#ffffff',
                borderColor: '#e5e7eb',
                borderWidth: 1,
                borderRadius: 4,
                textStyle: {
                    color: '#374151'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: ['0-2分', '2-4分', '4-6分', '6-8分', '8-10分', '10分以上'],
                axisLabel: {
                    color: '#6b7280',
                    fontSize: 12
                },
                axisLine: {
                    lineStyle: {
                        color: '#e5e7eb'
                    }
                }
            },
            yAxis: {
                type: 'value',
                name: '学生人数',
                nameTextStyle: {
                    color: '#6b7280'
                },
                axisLabel: {
                    color: '#6b7280',
                    fontSize: 12
                },
                axisLine: {
                    lineStyle: {
                        color: '#e5e7eb'
                    }
                },
                splitLine: {
                    lineStyle: {
                        color: '#f3f4f6'
                    }
                }
            },
            series: [{
                data: [30, 45, 78, 120, 95, 32],
                type: 'bar',
                itemStyle: {
                    color: '#2563eb',
                    borderRadius: [2, 2, 0, 0]
                },
                emphasis: {
                    itemStyle: {
                        color: '#1d4ed8'
                    }
                },
                barWidth: '60%'
            }]
        };

        this.charts.creditDistribution.setOption(option);
    }

    // 初始化院系学分达标率图表 - 简约设计
    initDepartmentCompletion() {
        const chartDom = document.getElementById('departmentCompletion');
        if (!chartDom) return;

        this.charts.departmentCompletion = echarts.init(chartDom);
        
        const option = {
            animation: true,
            animationDuration: 1000,
            animationEasing: 'ease',
            title: {
                text: '院系学分达标率',
                left: 'center',
                textStyle: {
                    fontSize: 16,
                    fontWeight: 'normal',
                    color: '#374151'
                }
            },
            tooltip: {
                trigger: 'item',
                backgroundColor: '#ffffff',
                borderColor: '#e5e7eb',
                borderWidth: 1,
                borderRadius: 4,
                textStyle: {
                    color: '#374151'
                },
                formatter: '{a} <br/>{b}: {c}% ({d}%)'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                textStyle: {
                    color: '#6b7280',
                    fontSize: 12
                }
            },
            series: [{
                name: '达标率',
                type: 'pie',
                radius: '50%',
                data: [
                    {value: 85, name: '计算机学院', itemStyle: {color: '#2563eb'}},
                    {value: 78, name: '电子信息学院', itemStyle: {color: '#64748b'}},
                    {value: 92, name: '自动化学院', itemStyle: {color: '#10b981'}},
                    {value: 68, name: '机械工程学院', itemStyle: {color: '#f59e0b'}},
                    {value: 88, name: '经济管理学院', itemStyle: {color: '#ef4444'}}
                ],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.2)'
                    }
                },
                label: {
                    color: '#374151',
                    fontSize: 12,
                    formatter: '{b}: {c}%'
                },
                labelLine: {
                    lineStyle: {
                        color: '#e5e7eb'
                    }
                }
            }]
        };

        this.charts.departmentCompletion.setOption(option);
    }

    // 绑定窗口大小变化事件
    bindResizeEvents() {
        window.addEventListener('resize', () => {
            Object.values(this.charts).forEach(chart => {
                if (chart && typeof chart.resize === 'function') {
                    chart.resize();
                }
            });
        });
    }

    // 更新图表数据
    updateChartData(chartName, newData) {
        if (this.charts[chartName]) {
            this.charts[chartName].setOption({
                series: [{
                    data: newData
                }]
            });
        }
    }

    // 添加动画效果
    addChartAnimations() {
        Object.values(this.charts).forEach(chart => {
            if (chart) {
                chart.setOption({
                    animation: true,
                    animationDuration: 1000,
                    animationEasing: 'ease'
                });
            }
        });
    }

    // 销毁图表
    destroy() {
        Object.values(this.charts).forEach(chart => {
            if (chart && typeof chart.dispose === 'function') {
                chart.dispose();
            }
        });
        this.charts = {};
    }
}

// 导出图表管理器
if (typeof module !== 'undefined' && module.exports) {
    module.exports = ChartManager;
} 